function LocationRibbonItem(location, startTime, endTime) {
    this.location = location;
    this.startTime = startTime;
    this.endTime = endTime;
}

LocationRibbonItem.prototype = {
    getLength: function() {
        return (this.endTime - this.startTime) / 3600000 / 24;
    }
};

///TODO: wyh - need to simplify this class
function LocationBar(monthModel) {
    this._monthModel = monthModel;
    this._locationRibbons = this._getLocationRibbons();
    this._departureDateTimes = this._getDepartureDateTimes();
    this._missingTravelDates = this._getMissingTravelDates();
}

LocationBar.prototype = {
    _getMissingTravelDates: function() {
        return this._monthModel.getFullMissingTravelDays();
    },
    _getDepartureDateTimes: function() {
        var days = this._monthModel.getFullDays();
        var departureDateTimes = [];
        days.foreach(function(day) {
            departureDateTimes = departureDateTimes.concat(day.getDepartureDateTimes());
        });
        return departureDateTimes;
    },
    _getLocationRibbons: function() {
        var dayWeekGroups = this._monthModel.getDaysGroupedByWeek();
        var groupedLocationRibbons = [];
        dayWeekGroups.foreach(function(group) {
            var locationInfos = [];
            group.foreach(function(day) {
                var locationInfosInDay = day.getLocationInfos();
                locationInfos = locationInfos.concat(locationInfosInDay);
            });

            var locationRibbons = [];
            var currentItem = {};
            var lastLocation = null;
            var lastDepartureTime = null;

            locationInfos.foreach(function(locationInfo) {
                if (locationInfo.location != lastLocation) {
                    var locationStartTime = lastDepartureTime || locationInfo.getDayStartTime();
                    var locationRibbonItem = new LocationRibbonItem(locationInfo.location, locationStartTime, locationInfo.getLocationEndTime());

                    currentItem = locationRibbonItem;
                    if (locationInfo.hasLocation()) {
                        locationRibbons.push(locationRibbonItem);
                    }
                } else {
                    currentItem.endTime = locationInfo.getLocationEndTime();
                }
                lastDepartureTime = locationInfo.departureTime;
                lastLocation = locationInfo.location;
            });
            groupedLocationRibbons = groupedLocationRibbons.concat(locationRibbons);
        });

        return groupedLocationRibbons;
    },

    _isSameLocation: function(bar1, bar2) {
        return bar1.location == bar2.location;
    },

    _shouldRenderLocationText: function(index, locationName) {
        var dayCellWidth = 94;
        var iconWidth = 18;
        var letterWidth = 7;
        var bar = this._locationRibbons[index];
        return (dayCellWidth * bar.getLength() - iconWidth) > locationName.length * letterWidth;
    },

    _getDayCellInfo: function(date) {
        var dayCell = $('#' + DayView.getDateCellId(date));
        return {
            left: dayCell.position().left,
            top: dayCell.position().top,
            width: dayCell.width(),
            height: dayCell.height(),
            right: dayCell.position().left + dayCell.width(),
            bottom: dayCell.position().top + dayCell.height(),
            dom: dayCell
        };
    },

    _getXPositonOfCellByTime: function(cell, date) {
        var totalMinutesInADay = 1440;
        return cell.left + cell.width * date.getMinutesOfDay() / totalMinutesInADay;
    },

    _renderLocationRibbon: function(dateRange, locationText, locationFullname, container) {
        var startCell = this._getDayCellInfo(dateRange.start);
        var endCell = this._getDayCellInfo(dateRange.end);

        var ribbonHeight = 12;
        var ribbonLeft = this._getXPositonOfCellByTime(startCell, dateRange.start);
        var ribbonWidth = this._getXPositonOfCellByTime(endCell, dateRange.end) - ribbonLeft;
        var ribbonTop = startCell.top + startCell.height / 2 - ribbonHeight / 2;

        var locationClass = 'location-ribbon';
        var locationRibbon = $('<span>')
                .text(locationText)
                .attr('title', locationFullname)
                .addClass(locationClass)
                .css('width', ribbonWidth)
                .css('height', ribbonHeight)
                .css('left', ribbonLeft)
                .css('top', ribbonTop)
                .css('z-index', CalendarZIndexManager.locationRibbonZIndex);
        locationRibbon.appendTo(container);
    },

    _renderLocationRibbons: function(container) {
        var me = this;
        var locationRibbonGroups = this._locationRibbons;
        for (var i = 0; i < locationRibbonGroups.length; i++) {
            var ribbon = locationRibbonGroups[i];

            var locationText = this._shouldRenderLocationText(i, ribbon.location.fullname) ? ribbon.location.fullname : '';

            me._renderLocationRibbon(new DateRange(ribbon.startTime, ribbon.endTime), locationText, ribbon.location.fullname, container);
        }
    },

    _renderTravelIcons: function(container) {
        var me = this;
        this._departureDateTimes.foreach(function(dateTime) {
            var dayCell = me._getDayCellInfo(dateTime);
            var travelIconClass = 'travel-icon';
            var travelIcon = $('<span>')
                    .addClass(travelIconClass)
                    .css('left', me._getXPositonOfCellByTime(dayCell, dateTime) - 9)
                    .css('top', dayCell.top + dayCell.height / 2 - 9)
                    .css('z-index', CalendarZIndexManager.locationRibbonZIndex);
            travelIcon.appendTo(container);
        });
    },

    _renderMissingTravelIcons: function(container) {
        var me = this;
        this._missingTravelDates = this._getMissingTravelDates();
        this._missingTravelDates.foreach(function(date) {
            var dayCell = me._getDayCellInfo(date);
            var missingTravelIconClass = 'missing-travel-icon';
            var missingTravelIcon = $('<span>')
                    .addClass(missingTravelIconClass)
                    .css('left', dayCell.left - 10)
                    .css('top', dayCell.top + dayCell.height / 2 - 10)
                    .css('z-index', CalendarZIndexManager.locationRibbonZIndex);
            missingTravelIcon.appendTo(container);
        });
    },

    render: function() {
        var container = $('<div/>');
        this._renderLocationRibbons(container);
        this._renderTravelIcons(container);
        this._renderMissingTravelIcons(container);
        return container;
    }
};

